/**
 * 同步版本
 * @param ms
 */
const sleep = (ms) => {
    const end = +new Date() + ms
    while (+new Date() < end) {
        /*do nothing*/
    }
}

const timer = () => {
    console.log('4')
    sleep(1000)
    console.log('5')
    console.log('6')
}

timer()


/**
 * 异步版本
 */

const sleep1 = (ms) => {
    return new Promise((resolve => setTimeout(resolve, ms)))
}

async function timer1() {
    console.log('res1')
    console.log('res2')
    sleep1(1000).then(() => {
        console.log('res3')
    })
    console.log('res4')
    await sleep1(2000)
    console.log('res5')
}

timer1()

/**
 * 同步版本
*/

function timer2 (ms) {
    const end = +new Date() + ms
    while(+new Date()<end) {
        // do nothing
    }
}

function timer3 (ms) {
    return new Promise(resolve => settimeout(() =>resolve,ms))
}


function _sleep(ms) {
  const endTime = +new Date() + ms;
  while (+new Date() < endTime) {
    // do nothing
  }
}

function print() {
  console.log(+new Date());
}

console.log("11111");

_sleep(print());

console.log("22222");
